<template>
  <div v-if="isFullScreen" class="browser-toolbar">
    <div class="tabs-section">
      <div class="tabs-spacer">
        <div class="tab active">
          <div class="tab-content">
            <img src="https://www.baidu.com/favicon.ico" alt="favicon" class="favicon">
            <span class="tab-title">{{ title }}</span>
          </div>
          <button class="close-tab"><i style="font-size: 12px;color: #232323;font-weight: 600" class="el-icon-close"></i></button>
        </div>
        <button class="new-tab">+</button>
      </div>
      <!-- Window Control Buttons -->
      <div class="window-controls">
        <button class="window-button minimize">
          <i style="font-size: 18px;color: #232323" class="el-icon-minus"></i>
        </button>
        <button class="window-button maximize">
          <i style="font-size: 18px;color: #232323" class="el-icon-folder-opened"></i>
        </button>
        <button class="window-button close">
          <i style="font-size: 18px;color: #232323" class="el-icon-close"></i>
        </button>
      </div>
    </div>
    <!-- Address Bar Section -->
    <div class="address-section">
      <button class="nav-button" @click="goBack">
        <i style="font-size: 18px;color: #232323" class="el-icon-back"></i>
      </button>
      <button class="nav-button" @click="goForward">
        <i style="font-size: 18px;color: #232323" class="el-icon-right"></i>
      </button>
      <button class="nav-button" @click="refreshPage">
        <i style="font-size: 18px;color: #232323" class="el-icon-refresh-right"></i>
      </button>
      <el-input type="text" v-model="displayUrl.addressName" @keyup.enter="onUrlChange" class="address-bar">
        <i slot="suffix" style="font-size: 18px;color: #232323" class="el-icon-star-off"></i>
      </el-input>
      <div class="right-button-group">
        <button class="right-button">
          <img src="./1 (1).png">
        </button>
        <button class="right-button">
          <img src="./2.png">
        </button>
        <button class="right-button">
          <i class="el-icon-more" style="transform: rotate(90deg); "></i>
        </button>
      </div>
    </div>
  </div>
</template>
<script>
import { EventBus } from '@/eventBus.js'; // 更改为正确的路径
import { getLocalStorageItem } from '@/utils/storage-helper';

export default {
  props: ['addressMapping'],
  data() {
    return {
      title: getLocalStorageItem('titleData'),
      displayUrl: getLocalStorageItem('addressMapping'), // 初始URL值
      isFullScreen: false // 初始全屏状态
    };
  },
  methods: {
    handleAddressMappingChanged(newAddressMapping,titleData) {
      this.displayUrl = newAddressMapping;
      this.title=titleData
    },
    onUrlChange() {
      this.$emit('change-url', this.displayUrl); // 发送新URL
    },
    goBack() {
      this.$emit('navigate', 'back');
    },
    goForward() {
      this.$emit('navigate', 'forward');
    },
    refreshPage() {
      this.$emit('navigate', 'refresh');
    },
    checkFullScreen() {
      this.isFullScreen =
          document.fullscreenElement ||
          document.mozFullScreenElement ||
          document.webkitFullscreenElement ||
          document.msFullscreenElement;
    }
  },
  mounted() {
    this.checkFullScreen();
    document.addEventListener('fullscreenchange', this.checkFullScreen);
    document.addEventListener('webkitfullscreenchange', this.checkFullScreen);
    document.addEventListener('mozfullscreenchange', this.checkFullScreen);
    document.addEventListener('msfullscreenchange', this.checkFullScreen);

    // 监听 eventBus 事件
    EventBus.$on('addressMappingChanged', this.handleAddressMappingChanged);
  },
  beforeDestroy() {
    document.removeEventListener('fullscreenchange', this.checkFullScreen);
    document.removeEventListener('webkitfullscreenchange', this.checkFullScreen);
    document.removeEventListener('mozfullscreenchange', this.checkFullScreen);
    document.removeEventListener('msfullscreenchange', this.checkFullScreen);

    // 销毁 eventBus 事件监听
    EventBus.$off('addressMappingChanged', this.handleAddressMappingChanged);
  }
};
</script>

<style scoped>
/* Your CSS styles go here */
.browser-toolbar {
  display: flex;
  flex-direction: column;
  background-color: #ffffff;
  border-bottom: 1px solid #d1d1d1;
  position: fixed;
  top: 0;
  width: 100vw;
  z-index: 1000;
}

.address-section {
  display: flex;
  align-items: center;
  height: 60px;
  background-color: white;
  border-bottom: 1px solid #ccc;
  padding: 0 10px;
  padding-top: 10px;
  padding-bottom: 8px;
}

.nav-button, .right-button {
  background: none;
  border: none;
  font-size: 16px;
  cursor: pointer;
  width: 40px;
}

.el-icon-star-off {
  font-weight: 600;
  position: absolute;
  top: 6px;
  right: 10px;
}

.address-bar {
  border: none;
  background: transparent;
}

.address-bar .el-input__inner {
  flex: 1;
  padding: 0px 10px;
  border: none;
  border-radius: 20px;
  margin: 0 10px;
  height: 34px;
  font-size: 16px;
  background: rgb(236, 239, 247);
  position: relative;
}

.right-button-group {
  display: flex;
  width: 110px;
}

.tabs-section {
  display: flex;
  align-items: center;
  height: 40px;
  padding: 0 8px;
  background: rgb(221, 227, 253);
}

.tabs-spacer {
  flex-grow: 1;
  display: flex;
  align-items: center;
  height: 32px;
  margin-top: 8px;
}

.tab {
  display: flex;
  align-items: center;
  height: 100%;
  background-color: #f1f1f1;
  border-top-left-radius: 6px;
  border-top-right-radius: 6px;
  border: 1px solid #d1d1d1;
  border-bottom: none;
  margin-right: 4px;
  padding: 0 8px;
  width: 260px;
  position: relative;
}

.tab.active {
  background-color: #ffffff;
}

.tab-content {
  display: flex;
  align-items: center;
}

.tab .favicon {
  width: 16px;
  height: 16px;
  margin-right: 8px;
}

.tab-title {
  font-size: 14px;
  white-space: nowrap;
}

.close-tab {
  background: none;
  border: none;
  font-size: 12px;
  margin-left: 8px;
  cursor: pointer;
  position: absolute;
  right: 10px;
  top: 4px;
}

.new-tab {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  height: 100%;
  display: flex;
  align-items: center;
  padding: 0px 8px;
}

.window-controls {
  display: flex;
  align-items: center;
}

.window-button {
  background: none;
  border: none;
  font-size: 16px;
  cursor: pointer;
  padding: 0 8px;
}

.window-button.minimize {
  font-size: 18px;
}

.window-button.maximize {
  font-size: 18px;
}

.window-button.close {
  color: red;
}
</style>
